package com.itheima.sys.controller;

import com.itheima.common.vo.Result;
import com.itheima.sys.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class SmsController {

    @Autowired
    private SmsService smsService;

    /**
     * 发送短信
     * @param mobile 手机号
     * @param type 短信类型  1：登录 2:注册 3：重置管理员 4：变更企业管理员
     * @throws ServletRequestBindingException
     */
    @GetMapping("/sms/code")
    public Result shendVerifyCode(@RequestParam("mobile") String mobile, @RequestParam("type") Integer type) throws ServletRequestBindingException {
        smsService.shendVerifyCode(mobile, type);
        return Result.successMessage("验证码发送成功，请查收！");
    }

    /**
     * 判断验证码是否正确
     * @param mobile 手机号
     * @param checkcode 用户填写验证码
     * @param type 验证码类型-用于选择不同redis key 前缀
     * @return
     */
    @GetMapping("/sms/verify")
    public Result<Boolean> verifyCheckcode(@RequestParam(value = "mobile", required = false) String mobile,
                                           @RequestParam(value = "checkcode", required = false) String checkcode,
                                           @RequestParam("type") Integer type) {
        return Result.success("校验结果",  smsService.verifyCheckcode(mobile, checkcode, type));
    }
}
